/*
 * This file is part of the eccOS Library.
 * Copyright (C) 2007.
 *
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 */
package org.openeccos.demo;

import java.util.Calendar;
import java.util.Random;

import nextapp.echo2.app.Extent;
import nextapp.echo2.app.event.ActionEvent;
import nextapp.echo2.app.event.ActionListener;

import org.openeccos.EccosAppInstance;
import org.openeccos.EccosDesktop;
import org.openeccos.EccosPerspective;
import org.openeccos.dao.EccosRepository;
import org.openeccos.dao.EccosService;
import org.openeccos.demo.action.CmdOpenDemoDialog;
import org.openeccos.demo.gui.FrmDemo;
import org.openeccos.demo.gui.WzdDemo;
import org.openeccos.gui.EccLE.DISPLAY_MODE;
import org.openeccos.model.MTask;
import org.openeccos.model.MUser;
import org.openeccos.util.EccosDefaultLookAndFeel;
import org.openeccos.util.ILookAndFeel;

import com.sas.framework.expojo.ModelExposer;

import echopointng.Menu;
import echopointng.MenuBar;
import echopointng.MenuItem;

/**
 * The default perspective for the Eccos Demo. The perspective defines the menus and the L&F
 * @author cgspinner@web.de
 */
public class DemoPerspective extends EccosPerspective {
	
	private ILookAndFeel colorScheme;
	
	@Override
	public ILookAndFeel getLookAndFeel() {
		if (colorScheme == null) {
			colorScheme = new EccosDefaultLookAndFeel() {
				public String getFooterLogo() {
					return "!eccOS Demo";  //prepend a "!" if you want a plain text logo
				}
				@Override
				public String getDesktopLogo() {
					return "img/eccOS.gif";
				}
			};
		}
		return colorScheme;
	}
	
	@Override
	public void updateMenu() {
		
		MenuBar menuBar = getMenuBar();
		menuBar.removeAll();

		//the menu might depend if and which user is logged in
		//In this demo we just accept any user
//		MUser user = PDDesktop.getCurrentUser();
		
		Menu mnu = getMenu("Demo");
		menuBar.add(mnu);
		
		MenuItem mit = getMenuItem("eccLE Demo 1");
		mit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				FrmDemo frm = new FrmDemo(DISPLAY_MODE.SIDE_SCROLL);
				frm.setWidth(new Extent(690));
				frm.setHeight(new Extent(470));
				desktop.addWindow(frm);
			}
		});
		mnu.add(mit);

		mit = getMenuItem("eccLE Demo 2");
		mit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				FrmDemo frm = new FrmDemo(DISPLAY_MODE.EXPANDABLE);
				frm.setWidth(new Extent(500));
				frm.setHeight(new Extent(530));
				desktop.addWindow(frm);
			}
		});
		mnu.add(mit);

		mit = getMenuItem("Wizard");
		mit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				desktop.addWindow(new WzdDemo());
			}
		});
		mnu.add(mit);

		mit = getMenuItem("Make Task");
		mit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				mitTaskClicked();
			}
		});
		mnu.add(mit);

		mit = getMenuItem("Make Shortcut");
		mit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				mitShortcutClicked();
			}
		});
		mnu.add(mit);

		mit = getMenuItem("Change Perspective");
		mit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				desktop.switchPerspective(new DemoPerspective2());
			}
		});
		mnu.add(mit);

	}

	protected void mitTaskClicked() {
		//create a new task
		MTask task = new MTask();
		task.setActionClass(CmdOpenDemoDialog.class.getCanonicalName());
		task.setAssignedUser(EccosDesktop.getCurrentUser());
		task.setCreatedBy(EccosDesktop.getCurrentUser());
		task.setCreatedDate(Calendar.getInstance().getTime());
		task.setName("New Task");
		task.setDescription("This is a demo task");
		
		//persist the shortcut
		EccosService service = (EccosService)ModelExposer.get().getService(EccosService.NAME);
		service.persist(task);
		
		//make the task visible, flash the new item
		desktop.refreshTaskDisplay(true);		
	}

	protected void mitShortcutClicked() {
		//create a shortcut, which is pretty similar to creating a task
		MTask task = new MTask();
		task.setActionClass(CmdOpenDemoDialog.class.getCanonicalName());
		task.setAssignedUser(EccosDesktop.getCurrentUser());
		task.setCreatedBy(EccosDesktop.getCurrentUser());
		task.setCreatedDate(Calendar.getInstance().getTime());
		task.setName("Hello World Shortcut");
		task.setShortcut(true);  //it's a shortcut, not a regular task
		task.setShortcutX(250 + new Random().nextInt(400));
		task.setShortcutY(50 + new Random().nextInt(120));
		
		//persist the shortcut
		EccosService service = (EccosService)ModelExposer.get().getService(EccosService.NAME);
		service.persist(task);
		
		//make the shortcut visible
		desktop.refreshTaskDisplay();		
	}

	@Override
	protected void onInit() {
		//create a virtual user for each IP
		EccosRepository repo = (EccosRepository)ModelExposer.get().getRepository(EccosRepository.NAME);
		String remoteIP = EccosAppInstance.getBrowserSession().getRemoteHost(); 
		MUser user = repo.findUserByLogin(remoteIP, "eccos");
		if (user == null) {
			user = new MUser();
			user.setLogin(remoteIP);
			user.setPassword("eccos");
			EccosService service = (EccosService)ModelExposer.get().getService(EccosService.NAME);
			service.persist(user);
		}
		EccosAppInstance.getInstance().getUserSession().doLogin(user.getLogin(), "eccos");
		desktop.updateLoginState();
	}	
}